<?php
namespace app\admin\controller;
use think\Request;
use think\Session;
use think\Db;
class Meeting extends Base
{
    //批量轮播图上传
    public function List_img_uploads($name,$sk,$request)
    {
        $path = 'public' . DS;
        $paths = 'uploads' . DS . $sk . DS . 'admin' . '/';
        $img['0']=request()->file($name.'1');
        $img['1']=request()->file($name.'2');
        $img['2']=request()->file($name.'3');
        foreach ($img as $key=>$file) {
            if(!empty($file)){
                $info = $file
                ->move(ROOT_PATH . $path . $paths);
                if ($info) {
                    $imgs[$key] = $paths . $info->getSaveName();
                } else {
                    err('', $file->getError());
                }
            }else{
                $imgs[$key]="";
            }
        }
        //返回数据
        $mg = json_encode($imgs);
        return $mg;
    }
    
    //分解轮播
    public function resolve_imgs($images,$new_image)
    {
        $images= json_decode($images);      //查询数据库有图片的情况下，转换成json格式
        $new_image=json_decode($new_image);  //把新图片替换成旧图片
        //图片替换操作
        if(!empty($images)){
            foreach ($new_image as $key=>$value) {
                //判断图片不为空的情况下修改
                if($new_image[$key]!=null && $new_image!=""){
                    $images[$key]=$new_image[$key];
                }else{//保持原有图片格式
                }
            }
        }else{
            $images=$new_image; //如果图片为空的话就直接用新图片替换成旧图片
        }
    
        $images=json_encode($images);
        return $images;
    }

    // 首页框架
    public function index()
    {
        if(Session::get('user_id') != 1004) {
            $this->redirect('admin/Meeting/login');
        }
        $auth = Session::get('user_auth');

        $this->assign('auth', $auth);

        $menu = $this->menu();
        $this->assign('menu', $menu);

        return $this->fetch();
    }
    
    // 登录
    public function login()
    {
        if(request()->isPost()) {

            $post = request()->only(['account', 'pwd'], 'post');

            if (empty($post['account'])) {
                err('请输入账号');
            }
            if (empty($post['pwd'])) {
                err('请输入密码');
            }

            if ($post['account'] == 'huiyixingchengbang') {
                $result = db('customer')->where(['username' => $post['account'],'id'=>1004])
                ->field("id, mobile, username, name, pwd, status, expire, rules")
                ->find();
                
                // 查找不到
                if(! $result) {
                    err('该账号不存在');
                }
                
                if($result['status'] != 1) {
                    err('账号已禁用，请联系管理员');
                }
     
                // 密码不正确
                if(md5($post['pwd']) != $result['pwd']) {
                    err('密码错误，请重新输入');
                }

                session('user_id', $result['id']);
                session("user_account", $result['username']);
                session('user_name', $result['name']);
                session('user_auth', 0);

                // 验证成功
                suc('验证成功');
            }else{
                $res = db('meeting_admin')->where(['account'=>$post['account']])->find();
                // 查找不到
                if(! $res) {
                    err('该账号不存在');
                }

                // 密码不正确
                if(md5($post['pwd']) != $res['password']) {
                    err('密码错误，请重新输入');
                }


                session('user_id', $res['admin_id']);
                session("user_account", $res['account']);
                session('user_name', $res['name']);
                session('user_auth', $res['id']);

                // 验证成功
                $this->success('登陆成功','meeting/index','',1);

            }
            
        }
        
        // 默认登录失败
        return $this->fetch();
    }

    // 菜单
    public function menu(){
    
        $adminSession = session('user_account');
        
        if($adminSession == 'huiyixingchengbang'){
            $menu = db('meeting_auth')->where(['parent_id'=>0,'type'=>0])->order('sort asc')->select();

            foreach($menu as $key=>$val){
                $menuChild = db('meeting_auth')->where(['parent_id'=>$val['id']])->order('sort asc')->select();
                $menu[$key]['menuChild'] = $menuChild;
            }
        }else{  
            $menu = db('meeting_auth')->where(['parent_id'=>0,'type'=>0])->order('sort asc')->select();

            foreach($menu as $key=>$val){
                $menuChild = db('meeting_auth')->where(['parent_id'=>$val['id']])->order('sort asc')->select();
                $menu[$key]['menuChild'] = $menuChild;
            }
            $getAdmin = db('meeting_admin')->where(['account'=>$adminSession])->find();

            $userMenu = db('meeting_auth_list')->where(['id'=>$getAdmin['grade']])->find();
            
            if(empty($userMenu)){
                return $menu = '';
            }
            $userMenu['auth'] = json_decode($userMenu['auth']);
                
            foreach($menu as $key1=>$val1){

                foreach($val1['menuChild'] as $key2=>$val2){
                    if(in_array($val2['id'],$userMenu['auth'])){
                        
                    }else{
                        unset($menu[$key1]['menuChild'][$key2]);
                    }
                }
            }

            foreach($menu as $key=>$val){
                if(empty($val['menuChild'])){
                    unset($menu[$key]);
                }
            }

        }
        return $menu;
    }
    
    // 登出
    public function logout()
    {
        session('user_id', null);
        session('user_account', null);
        session('user_name', null);
        session('user_auth', null);
        return $this->redirect('admin/Meeting/login');
    }
    
    // 欢迎页面
    public function welcome()
    {
        return $this->fetch();
    }

    // 角色列表
    public function authList()
    {
        $res = db('meeting_auth_list')->select();
        foreach ($res as $key => $value) {
            $res[$key]['auth'] = json_decode($value['auth'],true);
        }
        $this->assign('res', $res);

        return $this->fetch();
    }

    // 添加
    public function authAdd()
    {
        $info = db('meeting_auth')->where(['parent_id'=>0,'type'=>0])->select();
        foreach($info as $key=>$val){
            $menuChild = db('meeting_auth')->where(['parent_id'=>$val['id']])->order('sort asc')->select();
            $info[$key]['child_menu'] = $menuChild;
        }
        $this->assign('info', $info);
        if (request()->isPost()) {
            $post = input('post.');
            $post['auth'] = json_encode($post['auth']);
            $post['create_time'] = time();
            $res = db('meeting_auth_list')->insert($post);
            if ($res) {
                $this->success('添加成功','meeting/authList','',1);
            }else{
                $this->error('添加失败');
            }
        }
        return $this->fetch();
    }

    // 修改
    public function authEdit($id)
    {
        $auth = db('meeting_auth')->where(['parent_id'=>0,'type'=>0])->select();
        foreach($auth as $key=>$val){
            $menuChild = db('meeting_auth')->where(['parent_id'=>$val['id']])->order('sort asc')->select();
            $auth[$key]['child_menu'] = $menuChild;
        }
        $this->assign('auth', $auth);
        $info = db('meeting_auth_list')->where(['id'=>$id])->find();
        $info['auth'] = json_decode($info['auth']);
        $this->assign('info', $info);

        if (request()->isPost()) {
            $post = input('post.');

            $post['auth'] = json_encode($post['auth']);
            $res = db('meeting_auth_list')->update($post);
            if ($res) {
                $this->success('编辑成功');
            }else{
                $this->error('编辑失败');
            }
        }
        return $this->fetch();
    }

    // 删除
    public function authDel($id)
    {
        $res = db('meeting_auth_list')->where(['id'=>$id])->delete();
        if ($res) {
            $this->success('删除成功','meeting/authList','',1);
        }else{
            $this->error('删除失败');
        }
    }

    // 会议列表
    public function meetingList()
    {
        $res = db('meeting_details')
            ->where(['admin_id'=>1004])
            ->order('create_time desc')
            ->select();

        $this->assign('res', $res);
        return $this->fetch();
    }

    // 会议状态
    public function meetingStatus($id,$status)
    {
        $res = db('meeting_details')->where(['id'=>$id])->update(['status'=>$status]);
        if ($res) {
            $this->success('修改成功','meeting/meetingAdmin','',1);
        }else{
            $this->error('修改失败');
        }
    }

    // 会议详情
    public function meetingAdmin()
    {
        $res = db('meeting_details')
            ->alias('a')
            ->join('meeting_config w','a.id=w.config_name and w.status=1 ','LEFT')
            ->field('a.id,a.title,a.address,a.mobile,a.company,a.status,w.config_value,a.sign_date,a.sign_date_end,a.going_date,a.going_date_end,a.create_time')
            ->where(['a.admin_id'=>1004])
            ->select();

        $this->assign('res', $res);

        return $this->fetch();
    }

    // 新增
    public function meetingAdminAdd(Request $request)
    {
        if (request()->isPost()) {
            $sk = 1004;
            $post = input('post.');
            $data['title'] = $post['title'];
            $data['img'] = $this->List_img_uploads('img',$sk,$request);
            if (!empty($post['position'])) {
                $position = explode(',', $post['position']);
                $data['longitude'] = $position[1];
                $data['latitude'] = $position[0];
            }

            $data['contents'] = $post['contents'];
            $data['going_date'] = strtotime($post['going_date']);
            $data['going_date_end'] = strtotime($post['going_date_end']);
            $data['mobile'] = $post['mobile'];
            $data['about'] = $post['about'];
            $data['company'] = $post['company'];
            $data['address'] = $post['address'];
            $data['agenda'] = $post['agenda'];
            $data['content'] = $post['content'];
            $data['grade'] = json_encode($post['grade']);
            $data['admin_id'] = 1004;
            $data['create_time'] = time();
            $res = db('meeting_details')->insert($data);
            if ($res) {
                $this->success('添加成功','meeting/meetingAdmin','',1);
            }else{
                $this->error('添加失败');
            }
        }

        return $this->fetch();
    }

    // 修改
    public function meetingAdminEdit(Request $request,$id)
    {
        if (request()->isPost()) {
            $sk = 1004;
            $post = input('post.');
            
            $data['title'] = $post['title'];
            $mg = $this->List_img_uploads('img',$sk,$request);
            
            if (!empty($mg)){
                $id = input('id');
                $images = db('meeting_details')->where("id='{$id}'")->value('img');
                $resolve_imgs = $this->resolve_imgs($images,$mg);
                $data['img'] = $resolve_imgs;
            }
            

            $data['contents'] = $post['contents'];
            $data['going_date'] = strtotime($post['going_date']);
            $data['going_date_end'] = strtotime($post['going_date_end']);
            $data['address'] = $post['address'];
            $position = explode(',', $post['position']);
            $data['latitude'] = $position[0];
            $data['longitude'] = $position[1];
            $data['about'] = $post['about'];
            $data['mobile'] = $post['mobile'];
            $data['company'] = $post['company'];
            $data['agenda'] = $post['agenda'];
            $data['content'] = $post['content'];
            $data['grade'] = json_encode($post['grade']);
            
            $res = db('meeting_details')->where(['id'=>$post['id']])->update($data);
            if ($res) {
                $this->success('修改成功','meeting/meetingAdmin','',1);
            }else{
                $this->error('修改失败');
            }
        }else{
            $info = db('meeting_details')->where(['id'=>$id])->find();
            $info['img'] = json_decode($info['img']);
            $info['position'] = $info['latitude'].','.$info['longitude'];
            $info['grade'] = json_decode($info['grade']);

            $this->assign('info', $info);
        }

        return $this->fetch();
    }

    // 删除
    public function meetingAdminDel($id)
    {
        $res = db('meeting_details')->where(['id'=>$id])->delete();

        if ($res) {
            $this->success('删除成功','meeting/meetingAdmin','',1);
        }else{
            $this->error('删除失败');
        }
    }

    // 会议人员类别
    public function userClass()
    {
        $res = db('meeting_user_type')->where(['admin_id'=>1004])->order('create_time desc')->select();

        $this->assign('res', $res);

        return $this->fetch();
    }

    // 添加
    public function userClassAdd()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $post['admin_id'] = 1004;
            $post['create_time'] = time();
            $res = db('meeting_user_type')->insert($post);

            if ($res) {
                $this->success('添加成功','meeting/userClass','',1);
            }else{
                $this->error('添加失败');
            }
        }

        return $this->fetch();
    }

    // 修改
    public function userClassEdit($id)
    {
        $info = db('meeting_user_type')->where(['id'=>$id])->find();
        $this->assign('info', $info);
        if (request()->isPost()) {
            $post = input('post.');

            $res = db('meeting_user_type')->where(['id'=>$id])->update($post);
            if ($res) {
                $this->success('修改成功','meeting/userClass','',1);
            }else{
                $this->error('修改失败');
            }
        }

        return $this->fetch();
    }

    // 删除
    public function userClassDel($id)
    {
        $res = db('meeting_user_type')->where(['id'=>$id])->delete();

        if ($res) {
            $this->success('删除成功','meeting/userClass','',1);
        }else{
            $this->error('删除失败');
        }
    }

    // 嘉宾端会议详情
    // public function meetingUser()
    // {
    //     $res = db('meeting_details')->where(['admin_id'=>1004])->select();

    //     $this->assign('res', $res);

    //     return $this->fetch();
    // }

    // 导入嘉宾信息列表
    public function userReg()
    {
        $post = Request::instance()->param();
        if(!empty($post['title'])){
            $where['name|mobile|meeting_id'] = ['like','%'.$post['title'].'%'];
        }

        $where['admin_id'] = 1005;

        $res = db('meeting_user')->where($where)->select();
        $title = !empty($post['title'])?$post['title']:'';
        $this->assign('title',$title);
        $this->assign('res', $res);

        return $this->fetch();
    }

    // 删除
    public function userRegDel($id)
    {
        $res = db('meeting_user')->where(['id'=>$id])->delete();

        if ($res) {
            $this->success('删除成功','meeting/userReg','',1);
        }else{
            $this->error('删除失败');
        }
    }

    // 导入嘉宾信息
    public function userListReg()
    {
        return $this->fetch();
    }
    /**
     * 导入excel数据
     */
    public function inserExcel($id)
    {
        Vendor('phpexcel.phpexcel');
        //获取表单上传文件
        $file = request()->file('file_stu');
        if (!empty($file)) {
            $info = $file->validate(['ext' => 'xlsx'])->move(ROOT_PATH . 'public' . DS . 'uploads');

            if ($info) {
                //            echo $info->getFilename();
                $exclePath = $info->getSaveName();  //获取文件名
                $file_name = ROOT_PATH . 'public' . DS . 'uploads' . DS . $exclePath;   //上传文件的地址
                vendor("PHPExcel.PHPExcel");
                $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
                $obj_PHPExcel = $objReader->load($file_name, $encode = 'utf-8');  //加载文件内容,编码utf-8

                $fileNameArr = explode(".",$exclePath);
                $fileNameArr1 = $fileNameArr[count($fileNameArr) - 1];
                if ($fileNameArr1 =='xlsx') {
                    $objReader = new \PHPExcel_Reader_Excel2007();
                    $objExcel = $objReader ->load($file_name);
                } else if ($fileNameArr1 =='xls') {
                    $objReader = new \PHPExcel_Reader_Excel5();
                    $objExcel = $objReader->load($file_name);
                }
                $sheet = $objExcel->getSheet(0);
                $highestRow = $sheet->getHighestRow(); // 取得总行数
                $highestColumn = $sheet->getHighestColumn(); // 取得总列数*/
                
                $meeting_id = db('meeting_details')->where(['admin_id'=>1004,'status'=>1])->find();
                $data = [];
                $m = 0;

                for($i=2;$i<=$highestRow;$i++){
                    $data[$m]['meeting_id'] = $objExcel->getActiveSheet()->getCell("A".$i)->getCalculatedValue();
                    $data[$m]['name'] = $objExcel->getActiveSheet()->getCell("B".$i)->getCalculatedValue();
                    $data[$m]['mobile'] = $objExcel->getActiveSheet()->getCell("C".$i)->getCalculatedValue();
                    $data[$m]['type'] = $id;//会议类别ID
                    $data[$m]['create_time'] = time();
                    $data[$m]['admin_id'] = 1004;
                    $m++;
                }
                $list = db('meeting_user')->insertAll($data);
                if($list){
                    $this->success("导入成功",'/Admin/Meeting/userclass');
                }else{
                    $this->success("导入失败,请从新导入",'/Admin/Meeting/userclass');
                }
            }
        }else{
            echo '<script language="javascript">alert("请选择上传文件"); window.history.back(); </script> ';
        }
    }

    // 数据导出
        /**
    * exportexcel                               导出数据为excel表格
    * @ys_member_ranking_day                    数据表名
    * @param    array                           $tableheader 表头
    * @param    array                           $table 查询表格
    * @return   array                           $where 查询条件
    */
    public function export()
    {
        $data = Request::instance()->param();
        $where = [];
        if($_GET)
        {
            if(!empty($data['name'])){
                $where['name'] = ['like','%'.$data['name'].'%'];
            }
            if(!empty($data['grade'])){
                $where['grade'] = ['like','%'.$data['grade'].'%'];
            }
        }
        //引入PHPExcel库文件
        Vendor('phpexcel.phpexcel');
        //创建对象
        $excel = new \PHPExcel();
        //Excel表格式
        $letter = array('A','B','C','D','E','F','G','H','I','j','k');
        //表头数组
        $tableheader = array('会议ID','姓名','电话');

        //填充表头信息
        for($i = 0;$i < count($tableheader);$i++) {
        $excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");
        }

        //表格数组
        $where['admin_id'] = 1006;
        $data = db('meeting_user')
            ->where($where)
            ->field('meeting_id,name,mobile,position')
            ->select();
        
        //填充表格信息
        for ($i = 2;$i <= count($data) + 1;$i++) {
            $j = 0;
            foreach ($data[$i - 2] as $key=>$value) {
            $excel->getActiveSheet()->setCellValue("$letter[$j]$i","$value".' ');
            $j++;
            }
        }

        //创建Excel输入对象
        $write = new \PHPExcel_Writer_Excel2007($excel);
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");;
        header('Content-Disposition:attachment;filename='.'嘉宾信息导入模板'.time().'.xlsx');
        header("Content-Transfer-Encoding:binary");
        $write->save('php://output');
        exit;
    }

    public function userExport($meeting_id)
    {
        $post = Request::instance()->param();
        
        //引入PHPExcel库文件
        Vendor('phpexcel.phpexcel');
        //创建对象
        $excel = new \PHPExcel();
        //Excel表格式
        $letter = array('A','B','C','D','E','F','G','H','I','j','k','L','M');
        //表头数组
        $tableheader = array('会议ID','姓名','电话','性别','身份证','分会任职','城市','单位','职称','行程联系人姓名','行程联系人手机号码','是否注册','创建时间');

        //填充表头信息
        for($i = 0;$i < count($tableheader);$i++) {
        $excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");
        }

        //表格数组
        $where['meeting_id'] = $meeting_id;
        $where['admin_id'] = 1004;
        $data = db('meeting_user')
            ->where($where)
            ->field('meeting_id,name,mobile,gender,id_card,position,city,company,title,travel_contact,travel_mobile,register,create_time')
            ->select();
        foreach ($data as $key => $value) {
                $data[$key]['create_time'] = date('Y-m-d',$value['create_time']);
                if ($value['register'] == 0) {
                    $data[$key]['register'] = '未注册';
                }else{
                    $data[$key]['register'] = '已注册';
                }
            }    
        //填充表格信息
        for ($i = 2;$i <= count($data) + 1;$i++) {
            $j = 0;
            foreach ($data[$i - 2] as $key=>$value) {
            $excel->getActiveSheet()->setCellValue("$letter[$j]$i","$value"." ");
            $j++;
            }
        }

        //创建Excel输入对象
        $write = new \PHPExcel_Writer_Excel2007($excel);
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");;
        header('Content-Disposition:attachment;filename='.'嘉宾信息'.date('Y-m-d',time()).'.xlsx');
        header("Content-Transfer-Encoding:binary");
        $write->save('php://output');
        exit;       
    }

    // 会议调查问卷列表
    public function questionnaire($id)
    {
        $this->assign('id', $id);
        $res = db('meeting_questionnaire')->where(['meeting_id'=>$id])->select();
        foreach ($res as $key => $value) {
            if ($value['type'] == 1 || $value['type'] == 2){
                $res[$key]['select_content'] = $value['answer_a'].'、'.$value['answer_b'].'、'.$value['answer_c'].'、'.$value['answer_d'];
            }else{
                $res[$key]['select_content'] = $value['answer_a'].'空';
            }
            
        }

        $this->assign('res', $res);
        return $this->fetch();
    }

    // 添加
    public function questionnaireAdd()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $post['create_time'] = date("Y-m-d H:i:s");
            $res = db('meeting_questionnaire')->insert($post);  
            if ($res) {
                $this->success('添加成功', url('meeting/questionnaire',['id'=>$post['meeting_id']]));
            }else{
                $this->error('添加失败');
            }
        }
        $this->assign('meeting_id', input('param.id'));
        return $this->fetch();
    }

    // 编辑
    public function questionnaireEdit($id)
    {
        if (request()->isPost()) {
            $post = input('post.');
            $post['update_time'] = date("Y-m-d H:i:s");
            $res = db('meeting_questionnaire')->where(['id'=>$post['id']])->update($post);
            if ($res) {
                $this->success('修改成功', url('meeting/questionnaire',['id'=>$post['meeting_id']]));
            }else{
                $this->error('修改失败');
            }
        }
        
        $info = db('meeting_questionnaire')->where(['id'=>$id])->find();
        if ($info['type'] == 1){
            $info['type'] = "单选题";
        }elseif($info['type'] == 2){
            $info['type'] = "多选题";
        }else{
            $info['type'] = "填空题";
        }
        $this->assign('info', $info);
        return $this->fetch();
    }

    // 删除
    public function questionnaireDel($id,$meeting_id)
    {
        $res = db('meeting_questionnaire')->where(['id'=>$id])->delete();

        if ($res) {
            $this->success('删除成功',url('meeting/questionnaire',['id'=>$meeting_id]),'',1);
        }else{
            $this->error('删除失败');
        }
    }

    // 嘉宾列表
    public function userList()
    {
        $post = Request::instance()->param();

        $res = db('meeting_user')
            ->where(['admin_id'=>1004,'meeting_id'=>$post['id']])
            ->order('id desc')
            ->select();
            
        foreach ($res as $key => $value) {
            $travel = db('meeting_travel')->where(['user_id'=>$value['id']])->find();
            $res[$key]['hotel_id'] = $travel['hotel_id'];
            $res[$key]['go_money'] = $travel['go_money'];
            $res[$key]['go_service'] = $travel['go_service'];
            $res[$key]['back_money'] = $travel['back_money'];
            $res[$key]['back_service'] = $travel['back_service'];

            $class = db('meeting_user_type')->where(['id'=>$value['type']])->find();
            $res[$key]['type'] = $class['name'];
            $car = db('meeting_car')->where(['user_id'=>$travel['user_id']])->find();
            $res[$key]['car_id'] = $car['id'];
        }

        $this->assign('meeting_id', $post['id']);
        $this->assign('res', $res);
        return $this->fetch();
    }

    // 是否注册
    public function examine($id,$register)
    {
        $post = Request::instance()->param();
        $res = db('meeting_user')->where(['id'=>$id])->update(['register'=>$register]);

        if ($res) {
            $this->success('审核成功',url('meeting/userList',['id'=>$post['meeting_id']]),'',1);
        }else{
            $this->error('审核失败');
        }
    }

    // 添加
    public function userListAdd($meeting_id)
    {
        $type = db('meeting_user_type')->where(['admin_id'=>1004])->select();
        $this->assign('type', $type);
        $this->assign('meeting_id', $meeting_id);
        if (request()->isPost()) {
            $post = input('post.');
            $post['meeting_id'] = $post['meeting_id'];
            $post['admin_id'] = 1004;
            $post['create_time'] = time();

            $res = db('meeting_user')->insert($post);
            if ($res) {
            $this->success('添加成功',url('meeting/userList',['id'=>$post['meeting_id']]),'',1);
        }else{
            $this->error('添加失败');
        }
        }

        return $this->fetch();
    }

    // 修改
    public function userListEdit($id,$meeting_id)
    {
        $type = db('meeting_user_type')->where(['admin_id'=>1004])->select();
        $this->assign('type', $type);
        $info = db('meeting_user')->where(['id'=>$id])->find();
        $typeName = db('meeting_user_type')->where(['id'=>$info['type']])->find();
        $info['type_name'] = $typeName['name'];
        $this->assign('info', $info);
        $this->assign('meeting_id', $meeting_id);
        if (request()->isPost()) {
            $post = input('post.');

            $post['create_time'] = time();
            $res = db('meeting_user')->where(['id'=>$post['id']])->update($post);
            if ($res) {
                $this->success('编辑成功');
            }else{
                $this->error('编辑失败');
            }
        }

        return $this->fetch();
    }

    // 删除
    public function userListDel($id)
    {

        $info = db('meeting_user')->where(['id'=>$id])->find();
        if ($info) {
            $result = db('user')->where(['id'=>$info['user_id']])->find();
            
            if ($result) {
                db('user')->where(['id'=>$info['user_id']])->delete();
                $res = db('meeting_user')->where(['id'=>$id])->delete();
                if ($res) {
                    $this->success('删除成功');
                }else{
                    $this->error('删除失败');
                }
            }else{
                $res = db('meeting_user')->where(['id'=>$id])->delete();
                if ($res) {
                    $this->success('删除成功');
                }else{
                    $this->error('删除失败');
                }
            }
        }
    }
    // 导出 - 安排火车票
    public function trainDownload()
    {
        $post = Request::instance()->param();
        if ($post['vehicle'] == 1) {
            $title = '火车票';
        }else{
            $title = '机票';
        }

        //引入PHPExcel库文件
        Vendor('phpexcel.phpexcel');
        //创建对象
        $excel = new \PHPExcel();
        //Excel表格式
        $letter = array('A','B','C','D','E','F','G','H','I','j','k','L','M','N','O','P','Q','R','S','T','U','V');
        //表头数组
        $tableheader = array('会议ID','姓名','身份证号码','手机号','来程日期','来程航班','出发地','目的地','起飞时间','降落时间','回程日期','回程航班','出发地','目的地','起飞时间','降落时间','来程票价','来程服务费','其他','返程票价','返程服务费','其他'); 

        //填充表头信息
        for($i = 0;$i < count($tableheader);$i++) {
        $excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");
        }

        //表格数组
        $where['b.vehicle'] = $post['vehicle'];
        if (!empty($post['meeting'])) {
            $where['b.meeting_id'] = $post['meeting'];
        }
        $where['go_price'] = 1;
        $where['back_price'] = 1;
        $data = db('meeting_user')
            ->alias('a')
            ->join('applet_meeting_travel b','b.user_id=a.id')
            ->where($where)
            ->field('a.meeting_id,a.name,a.id_card,a.mobile,b.go_start_date,b.go_flight_number,b.go_start_addr,b.go_end_addr,go_start_time,go_end_time,back_end_date,back_flight_number,back_start_addr,back_end_addr,back_start_time,back_end_time,go_money,go_service,go_solo,back_money,back_service,back_solo')
            ->select();
        $meetingName = db('meeting_details')->where(['id'=>$post['meeting']])->find();
        if (empty($meetingName)) {
            $meetingName['title'] = '所有会议';
        }
        foreach ($data as $key => $value) {
            $data[$key]['go_start_date'] = date('Y-m-d',$value['go_start_date']);
            $data[$key]['back_end_date'] = date('Y-m-d',$value['back_end_date']);
            if ($data[$key]['go_solo'] == 1) {
                $data[$key]['go_solo'] = '自行出票，会后报销';
            }else{
                $data[$key]['go_solo'] = '';
            }
            if ($data[$key]['back_solo'] == 1) {
                $data[$key]['back_solo'] = '自行出票，会后报销';
            }else{
                $data[$key]['back_solo'] = '';
            }
            if (!empty($post['meeting'])) {
                $data[$key]['meeting_id'] = $meetingName['title'];
            }
            
        }
        //填充表格信息
        for ($i = 2;$i <= count($data) + 1;$i++) {
            $j = 0;
            foreach ($data[$i - 2] as $key=>$value) {
            $excel->getActiveSheet()->setCellValue("$letter[$j]$i","$value".' ');
            $j++;
            }
        }

        //创建Excel输入对象
        $write = new \PHPExcel_Writer_Excel2007($excel);
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");;
        header('Content-Disposition:attachment;filename='.$meetingName['title'].'-'.$title.'-'.date('Y-m-d',time()).'.xlsx');
        header("Content-Transfer-Encoding:binary");
        $write->save('php://output');
        exit;        
    }

    // 导出 - 嘉宾总表
    public function userDownload($meeting_id)
    {
        //引入PHPExcel库文件
        Vendor('phpexcel.phpexcel');
        //创建对象
        $excel = new \PHPExcel();
        //Excel表格式
        $letter = array('A','B','C','D','E','F','G','H','I','j','k','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','Aj','Ak','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ','BA','BB');
        //表头数组
        $tableheader = array('会议ID','姓名','性别','电话','身份证','职务','单位','城市',
            '交通工具','来程日期','来程航班','起飞时间','降落时间','出发地','目的地','返程日期','返程航班',
            '起飞时间','降落时间','出发地','目的地','酒店名称','房型','房号','入住时间','离店时间','酒店电话',
            '分房','付费方式','住宿费','费用总计','备注','用途','司机姓名','司机电话','车牌号','开始用车时间',
            '安排送机时间','出发地','目的地','是否完成','是否空驶','用途','司机姓名','司机电话','车牌号','开始用车时间',
            '安排送机时间','出发地','目的地','是否完成','是否空驶','来程出票状态','返程出票状态'); //54

        //填充表头信息
        for($i = 0;$i < count($tableheader);$i++) {
        $excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");
        }

        //表格数组
        
        $data = db('meeting_user')
            ->alias('a')
            ->join('applet_meeting_travel b','a.id=b.user_id')
            ->join('applet_meeting_hotel c','b.hotel_id=c.id')
            ->where(['a.meeting_id'=>$meeting_id,'a.admin_id'=>1004])
            ->field('a.meeting_id,a.name,a.gender,a.mobile,a.id_card,a.position,a.company,a.city,
                b.vehicle,b.go_start_date,b.go_flight_number,b.go_start_time,b.go_end_time,b.go_start_addr,b.go_end_addr,b.back_end_date,b.back_flight_number,b.back_start_time,b.back_end_time,b.back_start_addr,b.back_end_addr,b.id,b.go_money,b.back_money,b.go_solo,b.back_solo,
                (c.name) as hotel_name,c.type,c.number,c.start_time,c.end_time,(c.mobile) as hotel_mobile,c.housing,c.payment,c.expense,c.money,c.item
               ') 
            ->select();
  
        $car = db('meeting_car')
            ->field('purpose,name,mobile,number,start_time,arrange_time,start_addr,end_addr,status,eig_status,travel_id')
            ->select();
            
        
        foreach ($data as $key => $value) {
            $data[$key]['car'] = [];
            foreach ($car as $key1 => $value1) {
                if($value1['travel_id'] == $value['id'] && $value1['purpose'] != 2){
                    $data[$key]["car"][] = $value1;
                }
            }
        }
        
        $meeting = db('meeting_details')->where(['id'=>$meeting_id])->find();
        foreach ($data as $key => $value) {
            $data[$key]['meeting_id'] = $meeting['title'];
            if ($data[$key]['gender'] == 1) {
                $data[$key]['gender'] = '男';
            }else{
                $data[$key]['gender'] = '女';
            }
            if ($data[$key]['vehicle'] == 0) {
                $data[$key]['vehicle'] = '飞机';
            }elseif ($data[$key]['vehicle'] == 1) {
                $data[$key]['vehicle'] = '高铁';
            }elseif ($data[$key]['vehicle'] == 2) {
                $data[$key]['vehicle'] = '自理';
            }else{
                $data[$key]['vehicle'] = '本地';
            }
            if (!empty($data[$key]['go_start_date'])) {
                $data[$key]['go_start_date'] = date('Y-m-d',$data[$key]['go_start_date']);
            }
            if (!empty($data[$key]['back_end_date'])) {
                $data[$key]['back_end_date'] = date('Y-m-d',$data[$key]['back_end_date']);
            }
            if (!empty($data[$key]['start_time'])) {
                $data[$key]['start_time'] = date('Y-m-d H:i',$data[$key]['start_time']);
            }
            if (!empty($data[$key]['end_time'])) {
                $data[$key]['end_time'] = date('Y-m-d H:i',$data[$key]['end_time']);
            }


            if (!empty($data[$key]['car'][0])) {
                if ($data[$key]['car'][0]['purpose'] == 0) {
                    $data[$key]['car'][0]['purpose'] = '接机';
                    unset($data[$key]['car'][0]['arrange_time']);
                }else{
                    $data[$key]['car'][0]['purpose'] = '送机';
                }
                if (!empty($data[$key]['car'][0]['start_time'])) {
                    $data[$key]['car'][0]['start_time'] = date('Y-m-d H:i', $data[$key]['car'][0]['start_time']);
                }
                if (!empty($data[$key]['car'][0]['arrange_time'])) {
                    $data[$key]['car'][0]['arrange_time'] = date('Y-m-d H:i', $data[$key]['car'][0]['arrange_time']);
                }
                $data[$key]['go_purpose'] = $data[$key]['car'][0]['purpose'];
                $data[$key]['go_name'] = $data[$key]['car'][0]['name'];
                $data[$key]['go_mobile'] = $data[$key]['car'][0]['mobile'];
                $data[$key]['go_number'] = $data[$key]['car'][0]['number'];
                $data[$key]['start_time_go'] = $data[$key]['car'][0]['start_time'];
                $data[$key]['arrange_time_go'] = '无';
                $data[$key]['start_addr_go'] = $data[$key]['car'][0]['start_addr'];
                $data[$key]['end_addr_go'] = $data[$key]['car'][0]['end_addr'];
                $data[$key]['go_status'] = $data[$key]['car'][0]['status'];
                $data[$key]['go_eig_status'] = $data[$key]['car'][0]['eig_status'];
                if ($data[$key]['go_status'] == 0) {
                    $data[$key]['go_status'] = '是';
                }else{
                    $data[$key]['go_status'] = '否';
                }

                if ($data[$key]['go_eig_status'] == 0) {
                    $data[$key]['go_eig_status'] = '否';
                }else{
                    $data[$key]['go_eig_status'] = '是';
                }
            }
            if (!empty($data[$key]['car'][1])) {
                if ($data[$key]['car'][1]['purpose'] == 0) {
                    $data[$key]['car'][1]['purpose'] = '接机';
                    unset($data[$key]['car'][0]['arrange_time']);
                }else{
                    $data[$key]['car'][1]['purpose'] = '送机';
                }
                if (!empty($data[$key]['car'][1]['start_time'])) {
                    $data[$key]['car'][1]['start_time'] = date('Y-m-d H:i', $data[$key]['car'][1]['start_time']);
                }
                if (!empty($data[$key]['car'][1]['arrange_time'])) {
                    $data[$key]['car'][1]['arrange_time'] = date('Y-m-d H:i', $data[$key]['car'][1]['arrange_time']);
                }

                $data[$key]['back_purpose'] = $data[$key]['car'][1]['purpose'];
                $data[$key]['back_name'] = $data[$key]['car'][1]['name'];
                $data[$key]['back_mobile'] = $data[$key]['car'][1]['mobile'];
                $data[$key]['back_number'] = $data[$key]['car'][1]['number'];
                $data[$key]['start_time_back'] = $data[$key]['car'][1]['start_time'];
                $data[$key]['arrange_time_back'] = $data[$key]['car'][1]['arrange_time'];
                $data[$key]['start_addr_back'] = $data[$key]['car'][1]['start_addr'];
                $data[$key]['end_addr_back'] = $data[$key]['car'][1]['end_addr'];
                $data[$key]['back_status'] = $data[$key]['car'][1]['status'];
                $data[$key]['back_eig_status'] = $data[$key]['car'][1]['eig_status'];
                if ($data[$key]['back_status'] == 1) {
                    $data[$key]['back_status'] = '是';
                }else{
                    $data[$key]['back_status'] = '否';
                }

                if ($data[$key]['back_eig_status'] == 1) {
                    $data[$key]['back_eig_status'] = '否';
                }else{
                    $data[$key]['back_eig_status'] = '是';
                }
            }

            unset($data[$key]['car']);
            unset($data[$key]['id']);

            if (!empty($data[$key]['go_solo'])) {
                $data[$key]['go'] = '自行出票，会后报销';
            }else{
                if ($data[$key]['go_money'] != 0) {
                    $data[$key]['go'] = '已出票';
                }else{
                    $data[$key]['go'] = '行程确认中';
                }
            }

            if (!empty($data[$key]['back_solo'])) {
                $data[$key]['back'] = '自行出票，会后报销';
            }else{
                if ($data[$key]['back_money'] != 0) {
                    $data[$key]['back'] = '已出票';
                }else{
                    $data[$key]['back'] = '行程确认中';
                }
            }
            unset($data[$key]['go_money']);
            unset($data[$key]['go_solo']);
            unset($data[$key]['back_money']);
            unset($data[$key]['back_solo']);            
        }
        
        //填充表格信息
        for ($i = 2;$i <= count($data) + 1;$i++) {
            $j = 0;
            foreach ($data[$i - 2] as $key=>$value) {
            $excel->getActiveSheet()->setCellValue("$letter[$j]$i","$value".' ');
            $j++;
            }
        }

        //创建Excel输入对象
        $write = new \PHPExcel_Writer_Excel2007($excel);
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");;
        header('Content-Disposition:attachment;filename='.$meeting['title'].'-总表-'.date('Y-m-d',time()).'.xlsx');
        header("Content-Transfer-Encoding:binary");
        $write->save('php://output');
        exit;
    }

    // 设置任务书
    public function task()
    {
        $post = Request::instance()->param();

        $file = new \app\admin\logic\File();
        $data['task_url'] = $file->uploadFileAdminPro(1004);
        
        if (empty($data)) {
            $this->success('上传成功',url('meeting/userList',['id'=>$post['meeting_id']]),'',1);
        }

        $res = db('meeting_user')->where(['id'=>$post['id']])->update(['task_url'=>$data['task_url']]);
        if($res){
            $this->success('上传成功');
        }else{
            $this->error('上传失败');
        }

    }    

    // 会议数据分析
    public function dataAnalysis()
    {
        $post = Request::instance()->param();
        $info['hits'] = db('meeting_statistics')->where(['meeting_id'=>$post['id']])->sum('hits');
        $info['enrolments'] = db('meeting_statistics')->where(['meeting_id'=>$post['id']])->sum('enrolments');
        $info['signs'] = db('meeting_statistics')->where(['meeting_id'=>$post['id']])->sum('signs');
        $this->assign('info', $info);
        $res = db('meeting_statistics')->where(['meeting_id'=>$post['id']])->order('create_time desc')->select();
        $this->assign('res', $res);
        // 用车
        $meetingData['carAll'] = db('meeting_car')->where(['meeting_id'=>$post['id']])->count();
        $meetingData['carYes'] = db('meeting_car')->where(['meeting_id'=>$post['id'],'arrange_status'=>1])->count();
        $meetingData['carNo'] = $meetingData['carAll'] - $meetingData['carYes'];

        // 酒店
        $meetingData['hotelAll'] = db('meeting_travel')->where(['meeting_id'=>$post['id']])->count();
        $meetingData['hotelYes'] = db('meeting_travel')->where(['meeting_id'=>$post['id'],'hotel_id'=>['exp','is not null']])->count();
        $meetingData['hotelNo'] = $meetingData['hotelAll'] - $meetingData['hotelYes'];

        // 行程
        $meetingData['travelYes'] = db('meeting_travel')->where(['meeting_id'=>$post['id'],'go_flight_number'=>['exp','is not null']])->count();
        $meetingData['travelNo'] = db('meeting_travel')->where(['meeting_id'=>$post['id'],'go_flight_number'=>['exp','is null']])->count();
        $this->assign('meetingData', $meetingData);
        
        return $this->fetch();
    }

    // 酒店安排情况
    public function userHotel($id)
    {
        $info = db('meeting_travel')
            ->alias('a')
            ->join('applet_meeting_hotel w','a.hotel_id=w.id')
            ->where(['a.id'=>$id])
            ->find();
        
        echo '无记录';
        exit();
            
        if (!empty($info['latitude']) && !empty($info['longitude'])) {
            $info['position'] = $info['latitude'].','.$info['longitude'];
        }else{
            $info['position'] = '';
        }
        
        dump($info);die;
        $this->assign('info', $info);
            
        return $this->fetch();
    }

    // 车辆安排情况
    public function userCar($meeting_id,$user_id)
    {
        
        $res = db('meeting_car')->where(['user_id'=>$user_id,'meeting_id'=>$meeting_id])->select();
        $this->assign('res', $res);

        return $this->fetch();
    }

    // 修改
    public function userCarEdit($id)
    {
        $info = db('meeting_car')->where(['id'=>$id])->find();
        $this->assign('info', $info);

        if (request()->isPost()) {
            $post = input('post.');
            $post['start_time'] = strtotime($post['start_time']);
            $post['arrange_time'] = strtotime($post['arrange_time']);
            $res = db('meeting_car')->where(['id'=>$post['id']])->update($post);
            if ($res) {
                echo '<script language="javascript">alert("编辑成功"); window.history.back(-1); </script> ';
            }else{
                $this->error('修改失败');
            }

        }

        return $this->fetch();
    }


    // 行程列表
    public function travelList()
    {
        $res = db('meeting_travel')->where(['admin_id'=>1004])->order('id desc')->select();
        
        $this->assign('res', $res);
        return $this->fetch();
    }

    // 新增行程
    public function travelListAdd()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $user = db('meeting_user')->where(['id'=>$post['user_id']])->find();
            $post['name'] = $user['name'];
            $post['admin_id'] = 1004;
            $post['create_time'] = time();
            $post['go_start_date'] = strtotime($post['go_start_date']);
            $post['back_end_date'] = strtotime($post['back_end_date']);
            $post['mobile'] = $user['mobile'];
            $post['avatarurl'] = $user['avatarurl'];
            $post['meeting_id'] = $user['meeting_id'];
            $result = db('meeting_travel')->insertGetId($post);
            if ($result) {
                $res = db('meeting_user')->where(['id'=>$post['user_id']])->update(['travel_id'=>$result]);
                if ($res) {
                    echo '<script language="javascript">alert("添加成功"); window.history.back(-1); </script> ';
                }else{
                    $this->error('添加失败');
                }
            }
        }

        return $this->fetch();
    }

    // 修改行程
    public function travelListEdit($id)
    {
        $info = db('meeting_travel')->where(['id'=>$id])->find();

        $this->assign('info', $info);
        if (request()->isPost()) {
            $post = input('post.');
            if (!empty($post['go_start_date'])) {
                $post['go_start_date'] = strtotime($post['go_start_date']);
            }
            if (!empty($post['back_end_date'])) {
                $post['back_end_date'] = strtotime($post['back_end_date']);
            }
            $res = db('meeting_travel')->where(['id'=>$post['id']])->update($post);
            if ($res) {
                $this->success('编辑成功');
            }else{
                $this->error('编辑失败');
            }
        }
        return $this->fetch();
    }

    // 删除行程
    public function travelListDel($id)
    {
        $res = db('meeting_travel')->where(['id'=>$id])->delete();
        
        if (!$res) {
            $this->error('删除失败');
        }
        
        $this->success('删除成功');
    }

    // 通知机票/火车票/接机/送机
    public function Tell()
    {
        $post = Request::instance()->param();
        #通知出票火车票、机票
        if ($post['type'] == 1) {
            $res = db('meeting_travel')->where("id != ''")->update(['go_price'=>1,'back_price'=>1]);
            if ($res) {
                $this->success('通知成功','meeting/travelList');
            }else{
                $this->error('通知失败');
            }
        #通知接机
        }elseif ($post['type'] == 2) {
            $res = db('meeting_travel')->where("id != ''")->update(['go_car'=>1,'back_car'=>1]);
            if ($res) {
                $this->success('通知成功','meeting/travelList');
            }else{
                $this->error('通知失败');
            } 
        }
    }

    // 安排机票
    public function planeList()
    {
        $post = Request::instance()->param();
        $where = [];
        if(!empty($post['meeting'])){
            $where['a.meeting_id'] = ['like','%'.$post['meeting'].'%'];
        }
        $res = db('meeting_travel')
            ->alias('a')
            ->join('applet_meeting_user c','c.id=a.user_id')
            ->where(['a.admin_id'=>1004,'a.vehicle'=>1])
            ->where("a.back_price=1 AND a.go_price=1")
            ->field('a.name,a.id,a.user_id,a.mobile,a.go_start_date,a.go_flight_number,a.go_start_addr,a.go_end_addr,
                a.go_start_time,a.go_end_time,a.back_end_date,a.back_flight_number,a.back_start_addr,a.back_end_addr,
                a.back_start_time,a.back_end_time,a.item,a.create_time,a.go_money,a.go_service,
                a.back_money,a.back_service,c.id_card')
            ->order('a.create_time desc')
            ->select();

        $meeting = !empty($post['meeting'])?$post['meeting']:'';
        $this->assign('meeting',$meeting);
        $this->assign('res', $res);
        return $this->fetch();
    }

    // 来程 - 编辑
    public function goPlaneEdit($id)
    {
        $info = db('meeting_travel')->where(['id'=>$id])->find();
        $this->assign('info', $info);
        if (request()->isPost()) {
            $post = input('post.');
            $post['create_time'] = time();
            $res = db('meeting_travel')->where(['id'=>$post['id']])->update($post);

            //短信发送
            $arr = $this->sendNoteToMobile($info['mobile'],$post['content']);
     
            if ($res) {
                $this->success('发送成功','meeting/planeList','',1);
            }else{
                $this->error('发送失败');
            }
        }

        return $this->fetch();
    }

    // 返程 - 编辑
    public function backPlaneEdit($id)
    {
        $info = db('meeting_travel')->where(['id'=>$id])->find();
        $this->assign('info', $info);
        if (request()->isPost()) {
            $post = input('post.');
            $post['create_time'] = time();
            $res = db('meeting_travel')->where(['id'=>$post['id']])->update($post);
            
            //短信发送
            $arr = $this->sendNoteToMobile($info['mobile'],$post['content']);
            
            if ($res) {
                $this->success('发送成功','meeting/planeList','',1);
            }else{
                $this->error('发送失败');
            }
        }

        return $this->fetch();
    }

    // 退改机票
    public function planeChange()
    {
        $post = Request::instance()->param();
        $where = [];
        if(!empty($post['meeting'])){
            $where['a.meeting_id'] = ['like','%'.$post['meeting'].'%'];
        }
        $res = db('meeting_travel_copy')
            ->alias('a')
            ->join('applet_meeting_user c','c.id=a.user_id')
            ->where(['a.admin_id'=>1004,'a.vehicle'=>1])
            ->where("a.back_price=1 OR a.go_price=1")
            ->field('a.name,a.id,a.user_id,a.mobile,a.go_start_date,a.go_flight_number,a.go_start_addr,a.go_end_addr,
                a.go_start_time,a.go_end_time,a.back_end_date,a.back_flight_number,a.back_start_addr,a.back_end_addr,
                a.back_start_time,a.back_end_time,a.item,a.create_time,
                a.go_money_change,a.go_service_change,a.back_money_change,a.back_service_change,c.id_card')
            ->order('a.id desc')
            ->select();

        $meeting = !empty($post['meeting'])?$post['meeting']:'';
        $this->assign('meeting',$meeting);
        $this->assign('res', $res);
        return $this->fetch();
    }

    // 导出 - 安排火车票
    public function trainDownloads()
    {
        $post = Request::instance()->param();
        if ($post['vehicle'] == 1) {
            $title = '火车票';
        }else{
            $title = '机票';
        }

        //引入PHPExcel库文件
        Vendor('phpexcel.phpexcel');
        //创建对象
        $excel = new \PHPExcel();
        //Excel表格式
        $letter = array('A','B','C','D','E','F','G','H','I','j','k','L','M','N','O','P','Q','R','S','T','U','V');
        //表头数组
        $tableheader = array('会议ID','姓名','身份证号码','手机号','来程日期','来程航班','出发地','目的地','起飞时间','降落时间','回程日期','回程航班','出发地','目的地','起飞时间','降落时间','来程票价','来程服务费','其他','返程票价','返程服务费','其他'); 

        //填充表头信息
        for($i = 0;$i < count($tableheader);$i++) {
        $excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");
        }

        //表格数组
        $where['b.vehicle'] = $post['vehicle'];
        if (!empty($post['meeting'])) {
            $where['b.meeting_id'] = $post['meeting'];
        }
        $where['go_price'] = 1;
        $where['back_price'] = 1;
        $data = db('meeting_user')
            ->alias('a')
            ->join('applet_meeting_travel_copy b','b.user_id=a.id')
            ->where($where)
            ->field('a.meeting_id,a.name,a.id_card,a.mobile,b.go_start_date,b.go_flight_number,b.go_start_addr,b.go_end_addr,go_start_time,go_end_time,back_end_date,back_flight_number,back_start_addr,back_end_addr,back_start_time,back_end_time,go_money_change,go_service_change,go_solo,back_money_change,back_service_change,back_solo')
            ->select();

        $meetingName = db('meeting_details')->where(['id'=>$post['meeting']])->find();
        if (empty($meetingName)) {
            $meetingName['title'] = '所有会议';
        }
        foreach ($data as $key => $value) {
            if (!empty($value['go_start_date'])){
                $data[$key]['go_start_date'] = date('Y-m-d',$value['go_start_date']);
            }
            if (!empty($value['back_end_date'])){
                $data[$key]['back_end_date'] = date('Y-m-d',$value['back_end_date']);
            }
            
            if ($data[$key]['go_solo'] == 1) {
                $data[$key]['go_solo'] = '自行出票，会后报销';
            }else{
                $data[$key]['go_solo'] = '';
            }
            if ($data[$key]['back_solo'] == 1) {
                $data[$key]['back_solo'] = '自行出票，会后报销';
            }else{
                $data[$key]['back_solo'] = '';
            }
            if (!empty($post['meeting'])) {
                $data[$key]['meeting_id'] = $meetingName['title'];
            }
            
        }
        //填充表格信息
        for ($i = 2;$i <= count($data) + 1;$i++) {
            $j = 0;
            foreach ($data[$i - 2] as $key=>$value) {
            $excel->getActiveSheet()->setCellValue("$letter[$j]$i","$value".' ');
            $j++;
            }
        }

        //创建Excel输入对象
        $write = new \PHPExcel_Writer_Excel2007($excel);
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");;
        header('Content-Disposition:attachment;filename='.$meetingName['title'].'-'.$title.'-'.date('Y-m-d',time()).'.xlsx');
        header("Content-Transfer-Encoding:binary");
        $write->save('php://output');
        exit;        
    }


    // 来程 - 编辑
    public function goPlaneChangeEdit($id)
    {
        $info = db('meeting_travel_copy')->where(['id'=>$id])->find();
        $this->assign('info', $info);
        if (request()->isPost()) {
            $post = input('post.');
            $res = db('meeting_travel_copy')->where(['id'=>$post['id']])->update($post);
            if ($res) {
                $this->success('编辑成功','meeting/planeChange','',1);
            }else{
                $this->error('编辑失败');
            }
        }

        return $this->fetch();
    }

    // 返程 - 编辑
    public function backPlaneChangeEdit($id)
    {
        $info = db('meeting_travel_copy')->where(['id'=>$id])->find();
        $this->assign('info', $info);
        if (request()->isPost()) {
            $post = input('post.');
            $res = db('meeting_travel_copy')->where(['id'=>$post['id']])->update($post);
            if ($res) {
                $this->success('编辑成功','meeting/planeChange','',1);
            }else{
                $this->error('编辑失败');
            }
        }

        return $this->fetch();
    }

    // 账单导出
    public function exportOrder()
    {
        $post = Request::instance()->param();
        if ($post['vehicle'] == 1) {
            $title = '火车票';
        }else{
            $title = '机票';
        }

        //引入PHPExcel库文件
        Vendor('phpexcel.phpexcel');
        //创建对象
        $excel = new \PHPExcel();
        //Excel表格式
        $letter = array('A','B','C','D','E','F','G','H','I','j','k','L','M','N','O','P','Q');
        //表头数组
        $tableheader = array('会议名称','嘉宾姓名','嘉宾手机号','交通工具','出发地','来程日期','航班号','来程票价','来程服务费','起飞时间','目的地','回程日期','航班号','返程票价','返程服务费','降落时间','创建时间'); 

        //填充表头信息
        for($i = 0;$i < count($tableheader);$i++) {
        $excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");
        }

        //查询字段
        $time = date("Y-m-d");
        
        if ($post['type'] == 1 and $post['vehicle'] == 2){
            $field = 'meeting_id,name,mobile,vehicle,go_start_addr,go_start_date,go_flight_number,go_money,go_service,go_start_time,go_end_addr,back_end_date,back_flight_number,back_money,back_service,go_end_time,create_time';
            $where['vehicle'] = 1;
            $data = db('meeting_travel')->field($field)->where($where)->select();
            $name = "机票";
        }elseif ($post['type'] == 2 and $post['vehicle'] == 2){
            $field = 'meeting_id,name,mobile,vehicle,go_start_addr,go_start_date,go_flight_number,go_money_change,go_service_change,go_start_time,go_end_addr,back_end_date,back_flight_number,back_money_change,back_service_change,go_end_time,create_time';
            $where['vehicle'] = 1;
            $data = db('meeting_travel_copy')->field($field)->where($where)->select();
            $name = "机票";
        }elseif ($post['type'] == 1 and $post['vehicle'] == 1){
            $field = 'meeting_id,name,mobile,vehicle,go_start_addr,go_start_date,go_flight_number,go_money,go_service,go_start_time,go_end_addr,back_end_date,back_flight_number,back_money,back_service,go_end_time,create_time';
            $where['vehicle'] = 2;
            $data = db('meeting_travel')->field($field)->where($where)->select();
            $name = "火车票";
        }elseif ($post['type'] == 2 and $post['vehicle'] == 1){
            $field = 'meeting_id,name,mobile,vehicle,go_start_addr,go_start_date,go_flight_number,go_money_change,go_service_change,go_start_time,go_end_addr,back_end_date,back_flight_number,back_money_change,back_service_change,go_end_time,create_time';
            $where['vehicle'] = 2;
            $data = db('meeting_travel_copy')->field($field)->where($where)->select();
            $name = "火车票";
        }
        
        //根据需求获得会议名称
        $meeting_name = "";
        
        foreach ($data as $k => $v){
            $arr = db('meeting_details')->field('title')->where(['id'=>$v['meeting_id']])->find();
            if ($arr){
                $data[$k]['meeting_id'] = $arr['title'];
                $meeting_name = $arr['title'];
            }
            
            if ($v['vehicle'] == 1){
                $data[$k]['vehicle'] = "飞机";
            }elseif ($v['vehicle'] == 2){
                $data[$k]['vehicle'] = "高铁";
            }
            
            if (!empty($v['back_end_date'])){
                $data[$k]['back_end_date'] = date("Y-m-d",$v['back_end_date']);
            }
            if (!empty($v['go_start_date'])){
                $data[$k]['go_start_date'] = date("Y-m-d",$v['go_start_date']);
            }
            if (!empty($v['create_time'])){
                $data[$k]['create_time'] = date("Y-m-d H:i:s",$v['create_time']);
            }
            
        }

        
        
        
        //填充表格信息
        for ($i = 2;$i <= count($data) + 1;$i++) {
            $j = 0;
            foreach ($data[$i - 2] as $key=>$value) {
            $excel->getActiveSheet()->setCellValue("$letter[$j]$i","$value".' ');
            $j++;
            }
        }

        //创建Excel输入对象
        $write = new \PHPExcel_Writer_Excel2007($excel);
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");;
       header("Content-Disposition:attachment;filename=$meeting_name.$name.$time.xlsx");
        header("Content-Transfer-Encoding:binary");
        $write->save('php://output');
        exit;
    }

    // 安排火车票
    public function trainList()
    {
        $post = Request::instance()->param();
        $where = [];
        if(!empty($post['meeting'])){
            $where['a.meeting_id'] = ['like','%'.$post['meeting'].'%'];
        }
        $where['a.go_price'] = 1;
        $where['a.back_price'] = 1;
        $res = db('meeting_travel')
            ->alias('a')
            ->join('applet_meeting_user c','c.id=a.user_id')
            ->where(['a.admin_id'=>1004,'a.vehicle'=>2])
            ->where($where)
            ->field('a.name,a.meeting_id,a.id,a.user_id,a.mobile,a.go_start_date,a.go_flight_number,a.go_start_addr,a.go_end_addr,
                a.go_start_time,a.go_end_time,a.back_end_date,a.back_flight_number,a.back_start_addr,a.back_end_addr,
                a.back_start_time,a.back_end_time,a.item,a.create_time,a.go_money,a.go_service,
                a.back_money,a.back_service,c.id_card,a.go_solo,a.back_solo')
            ->order('a.create_time desc')
            ->select();
        $meeting = !empty($post['meeting'])?$post['meeting']:'';
        $this->assign('meeting',$meeting);
        $this->assign('res', $res);
        return $this->fetch();        
    }

    // 来程 - 编辑
    public function goTrainEdit($id)
    {
        $info = db('meeting_travel')->where(['id'=>$id])->find();
        $this->assign('info', $info);
        if (request()->isPost()) {
            $post = input('post.');
            if (!empty($post['go_solo'])) {
                $post['go_solo'] = 1;
            }
            $res = db('meeting_travel')->where(['id'=>$post['id']])->update($post);
            if ($res) {
                $this->success('编辑成功','meeting/TrainList','',1);
            }else{
                $this->error('编辑失败');
            }
        }

        return $this->fetch();
    }

    // 返程 - 编辑
    public function backTrainEdit($id)
    {
        $info = db('meeting_travel')->where(['id'=>$id])->find();
        $this->assign('info', $info);
        if (request()->isPost()) {
            $post = input('post.');
            if (!empty($post['back_solo'])) {
                $post['back_solo'] = 1;
            }
            $res = db('meeting_travel')->where(['id'=>$post['id']])->update($post);
            if ($res) {
                $this->success('编辑成功','meeting/TrainList','',1);
            }else{
                $this->error('编辑失败');
            }
        }

        return $this->fetch();
    }

    // 退改火车票
    public function trainChange()
    {
        $post = Request::instance()->param();
        $where = [];
        if(!empty($post['meeting'])){
            $where['a.meeting_id'] = ['like','%'.$post['meeting'].'%'];
        }
        $res = db('meeting_travel_copy')
            ->alias('a')
            ->join('applet_meeting_user c','c.id=a.user_id')
            ->where(['a.admin_id'=>1004,'a.vehicle'=>2])
            ->where("a.back_price=1 OR a.go_price=1")
            ->field('a.name,a.id,a.user_id,a.mobile,a.go_start_date,a.go_flight_number,a.go_start_addr,a.go_end_addr,
                a.go_start_time,a.go_end_time,a.back_end_date,a.back_flight_number,a.back_start_addr,a.back_end_addr,
                a.back_start_time,a.back_end_time,a.item,a.create_time,
                a.go_money_change,a.go_service_change,a.back_money_change,a.back_service_change,c.id_card')
            ->order('a.create_time desc')
            ->select();
        $meeting = !empty($post['meeting'])?$post['meeting']:'';
        $this->assign('meeting',$meeting);
        $this->assign('res', $res);
        return $this->fetch();
    }

    // 来程 - 编辑
    public function gotrainChangeEdit($id)
    {
        $info = db('meeting_travel_copy')->where(['id'=>$id])->find();
        $this->assign('info', $info);
        if (request()->isPost()) {
            $post = input('post.');
            if (!empty($post['back_solo'])) {
                $post['back_solo'] = 1;
            }
            $res = db('meeting_travel_copy')->where(['id'=>$post['id']])->update($post);
            if ($res) {
                $this->success('编辑成功','meeting/trainChange','',1);
            }else{
                $this->error('编辑失败');
            }
        }

        return $this->fetch();
    }

    // 返程 - 编辑
    public function backtrainChangeEdit($id)
    {
        $info = db('meeting_travel_copy')->where(['id'=>$id])->find();
        $this->assign('info', $info);
        if (request()->isPost()) {
            $post = input('post.');
            $res = db('meeting_travel_copy')->where(['id'=>$post['id']])->update($post);
            if ($res) {
                $this->success('编辑成功','meeting/trainChange','',1);
            }else{
                $this->error('编辑失败');
            }
        }

        return $this->fetch();
    }

    // 删除
    public function trainChangeDel($id)
    {
        $res = db('meeting_travel_copy')->where(['id'=>$id])->delete();
        if ($res) {
            $this->success('删除成功','meeting/trainChange','',1);
        }else{
            $this->error('删除失败');
        }
    }

    

    // 导出 - 分房单
    public function housingDownload()
    {
        $post = Request::instance()->param();
        $where = [];
        //引入PHPExcel库文件
        Vendor('phpexcel.phpexcel');
        //创建对象
        $excel = new \PHPExcel();
        //Excel表格式
        $letter = array('A','B','C','D','E','F','G','H','I','j','K');
        //表头数组
        $tableheader = array('酒店名称','姓名','入住日期','离店日期','房型','拼住人','房号','付费方式','住宿费','费用总计','备注'); 

        //填充表头信息
        for($i = 0;$i < count($tableheader);$i++) {
        $excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");
        }

        //表格数组
        if (!empty($post['meeting'])) {
            $where['b.meeting_id'] = $post['meeting'];
        }

        $data = db('meeting_user')
            ->alias('a')
            ->join('applet_meeting_travel b','b.user_id=a.id')
            ->join('applet_meeting_hotel c','c.id=b.hotel_id','LEFT')
            ->where($where)
            ->field('c.name,(a.name) as user_name,c.start_time,c.end_time,c.type,c.housing,c.number,c.payment,c.expense,c.money,c.item')
            ->order('b.create_time desc')
            ->select();
            
        $meetingName = db('meeting_details')->where(['id'=>$post['meeting']])->find();
        if (empty($meetingName)) {
            $meetingName['title'] = '所有会议';
        }
        foreach ($data as $key => $value) {
            $data[$key]['start_time'] = date('Y-m-d',$value['start_time']);
            $data[$key]['end_time'] = date('Y-m-d',$value['end_time']);
        }
        //填充表格信息
        for ($i = 2;$i <= count($data) + 1;$i++) {
            $j = 0;
            foreach ($data[$i - 2] as $key=>$value) {
            $excel->getActiveSheet()->setCellValue("$letter[$j]$i","$value".' ');
            $j++;
            }
        }

        //创建Excel输入对象
        $write = new \PHPExcel_Writer_Excel2007($excel);
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");;
        header('Content-Disposition:attachment;filename='.$meetingName['title'].'-分房单-'.date('Y-m-d',time()).'.xlsx');
        header("Content-Transfer-Encoding:binary");
        $write->save('php://output');
        exit;        
    }

    // 导出 - 安排酒店
    public function hotelDownload()
    {
        $post = Request::instance()->param();
        $where = [];
        //引入PHPExcel库文件
        Vendor('phpexcel.phpexcel');
        //创建对象
        $excel = new \PHPExcel();
        //Excel表格式
        $letter = array('A','B','C','D','E','F','G','H','I','j','k','L','M','N');
        //表头数组
        $tableheader = array('会议ID','行程ID','姓名','性别','单位','身份证号码','手机号','来程日期','回程日期','酒店安排','入住房型','分房','房号','备注'); 

        //填充表头信息
        for($i = 0;$i < count($tableheader);$i++) {
        $excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");
        }

        //表格数组
        if (!empty($post['meeting'])) {
            $where['b.meeting_id'] = $post['meeting'];
        }

        $data = db('meeting_user')
            ->alias('a')
            ->join('applet_meeting_travel b','b.user_id=a.id')
            ->join('applet_meeting_hotel c','c.id=b.hotel_id','LEFT')
            ->where($where)
            ->field('a.meeting_id,a.id,a.name,a.gender,a.company,a.id_card,a.mobile,b.go_start_date,b.back_end_date,(c.name) as hotel_namec,c.type,c.housing,c.number,c.item')
            ->select();
            
        $meetingName = db('meeting_details')->where(['id'=>$post['meeting']])->find();
        if (empty($meetingName)) {
            $meetingName['title'] = '所有会议';
        }
        foreach ($data as $key => $value) {
            $data[$key]['go_start_date'] = date('Y-m-d',$value['go_start_date']);
            $data[$key]['back_end_date'] = date('Y-m-d',$value['back_end_date']);

            if (!empty($post['meeting'])) {
                $data[$key]['meeting_id'] = $meetingName['title'];
            }

            if ($data[$key]['gender'] == 1) {
                $data[$key]['gender'] = '男';
            }else{
                $data[$key]['gender'] = '女';
            }
            
        }
        //填充表格信息
        for ($i = 2;$i <= count($data) + 1;$i++) {
            $j = 0;
            foreach ($data[$i - 2] as $key=>$value) {
            $excel->getActiveSheet()->setCellValue("$letter[$j]$i","$value".' ');
            $j++;
            }
        }

        //创建Excel输入对象
        $write = new \PHPExcel_Writer_Excel2007($excel);
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");;
        header('Content-Disposition:attachment;filename='.$meetingName['title'].'-酒店安排-'.date('Y-m-d',time()).'.xlsx');
        header("Content-Transfer-Encoding:binary");
        $write->save('php://output');
        exit;        
    }

    // 酒店信息列表
    public function hotelArrange()
    {
        $post = Request::instance()->param();
        $where = [];
        if(!empty($post['name'])){
            $where['a.name'] = ['like','%'.$post['name'].'%'];
        }
        $res = db('meeting_travel')
            ->alias('a')
            ->join('applet_meeting_user w','w.id=a.user_id')
            ->join('applet_meeting_hotel c','c.id=a.hotel_id','LEFT')
            ->where($where)
            ->where(['a.admin_id'=>$this->accountId])
            ->order('a.create_time desc')
            ->order('a.go_start_date desc')
            ->field('a.id,a.meeting_id,w.name,w.gender,w.company,w.id_card,w.mobile,a.go_start_date,a.back_end_date,a.create_time,a.hotel_id,c.item,a.go_end_time,(c.name) as hotel_name,c.type,c.number,(c.id) as hotel_id')
            ->select();

        foreach ($res as $k => $v){
            if ($v['gender'] == 1){
                $res[$k]['gender'] = "男";
            }else{
                $res[$k]['gender'] = "女";
            }
        }
        $meeting = !empty($post['meeting'])?$post['meeting']:'';
        $this->assign('meeting',$meeting);
        $this->assign('res', $res);
        return $this->fetch();
    }
    /**
     * 导入酒店安排数据
     */
    public function inserHotel()
    {
        Vendor('phpexcel.phpexcel');
        //获取表单上传文件
        $file = request()->file('file_stu');
        if (!empty($file)) {
            $info = $file->validate(['ext' => 'xlsx'])->move(ROOT_PATH . 'public' . DS . 'uploads');

            if ($info) {
                
                $exclePath = $info->getSaveName();  //获取文件名
                $file_name = ROOT_PATH . 'public' . DS . 'uploads' . DS . $exclePath;   //上传文件的地址
                vendor("PHPExcel.PHPExcel");
                $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
                $obj_PHPExcel = $objReader->load($file_name, $encode = 'utf-8');  //加载文件内容,编码utf-8

                $fileNameArr = explode(".",$exclePath);
                $fileNameArr1 = $fileNameArr[count($fileNameArr) - 1];
                if ($fileNameArr1 =='xlsx') {
                    $objReader = new \PHPExcel_Reader_Excel2007();
                    $objExcel = $objReader ->load($file_name);
                } else if ($fileNameArr1 =='xls') {
                    $objReader = new \PHPExcel_Reader_Excel5();
                    $objExcel = $objReader->load($file_name);
                }
                $sheet = $objExcel->getSheet(0);
                $highestRow = $sheet->getHighestRow(); // 取得总行数
                $highestColumn = $sheet->getHighestColumn(); // 取得总列数*/
                
                $data = [];
                $m = 0;
                for($i=2;$i<=$highestRow;$i++){
                    $data[$m]['meeting_id'] = $objExcel->getActiveSheet()->getCell("A".$i)->getCalculatedValue();
                    $data[$m]['travel_id'] = $objExcel->getActiveSheet()->getCell("B".$i)->getCalculatedValue();
                    $data[$m]['mobile'] = $objExcel->getActiveSheet()->getCell("C".$i)->getCalculatedValue();
                    $data[$m]['position'] = $objExcel->getActiveSheet()->getCell("F".$i)->getCalculatedValue();
                    $data[$m]['create_time'] = time();
                    $data[$m]['admin_id'] = 1004;
                    $m++;
                }

                #2018/1/5修改
                #查询数据表数据，判断导入数据，如果重复就覆盖旧数据(逻辑问题先不改)
//                 $arr = db('meeting_user')->select();
//                 foreach ($data as $k => $v){
//                     foreach ($arr as $key => $val){
//                         $data = [];
//                         $data['meeting_id'] = $v['meeting_id'];
//                         $data['name'] = $v['mobile'];
//                         $data['id_card'] = $v['position'];
//                         $data['create_time'] = $v['create_time'];
//                         $data['admin_id'] = $v['admin_id'];
//                         if ($v['mobile'] != $val['name']){
//                             db('meeting_user')->insert($data);
//                         }else{
//                             db('meeting_user')->where(['id'=>$val['id']])->updata($v);
//                         }
//                     }
//                 }
                
                $list = db('meeting_user')->insertAll($data);
                if($list){
                    echo '<script language="javascript">alert("导入成功"); window.history.back(-1); </script> ';
                }else{
                    echo '<script language="javascript">alert("导入成功"); window.history.back(-1); </script> ';
                }
            }
        }else{
            echo '<script language="javascript">alert("请选择上传文件"); window.history.back(); </script> ';
        }
    }    

    // 安排酒店
    public function travelHotel($id)
    {
        $hotel = db('meeting_hotel')->where(['admin_id'=>1004])->select();
        $info = db('meeting_travel')->where(['id'=>$id])->find();
        $this->assign('info', $info);
        $this->assign('hotel', $hotel);
        if (request()->isPost()) {
            $post = input('post.');
            $res = db('meeting_travel')->where(['id'=>$id])->update(['hotel_id'=>$post['hotel_id']]);
            if ($res) {
                $data['start_time'] = strtotime($post['start_time']);
                $data['end_time'] = strtotime($post['end_time']);
                $data['status'] = 1;
                $data['housing'] = $post['housing'];
                $data['payment'] = $post['payment'];
                $data['expense'] = $post['expense'];
                $data['money'] = $post['money'];
                $data['item'] = $post['item'];
                db('meeting_hotel')->where(['id'=>$post['hotel_id']])->update($data);
                $this->success('修改成功','meeting/hotelArrange','',1);
            }else{
                $this->error('修改失败');   
            }
        }

        return $this->fetch();
    }

    // 修改
    public function hotelarrangeedit($id)
    {
        $info = db('meeting_hotel')->where(['id'=>$id])->find();
        $hotel = db('meeting_hotel')->where(['admin_id'=>1004])->select();
        $this->assign('hotel', $hotel);
        if (request()->isPost()) {
            $post = input('post.');
            $post['start_time'] = strtotime($post['start_time']);
            $post['end_time'] = strtotime($post['end_time']);
            $res = db('meeting_hotel')->where(['id'=>$post['id']])->update($post);
            if ($res) {
                $this->success('修改成功','meeting/hotelArrange','',1);
            }else{
                $this->error('修改失败');   
            }
        }
        // dump($info);die;
        $this->assign('info', $info);
        return $this->fetch();
    }
    // 行程列表 - 接机
    public function carArrangeGo()
    {
        $res = db('meeting_travel')
            ->alias('a')
            ->join('applet_meeting_user w','w.id=a.user_id')
            ->join('applet_meeting_hotel c','c.id=a.hotel_id','LEFT')
            ->join('applet_meeting_car d','d.travel_id=a.id and d.purpose=0','LEFT')
            ->where(['a.admin_id'=>1004,'a.go_car'=>1])
            ->order('a.create_time desc')
            ->field('a.id,a.meeting_id,w.name,w.gender,w.company,w.id_card,w.mobile,a.go_start_date,a.back_end_date,a.create_time,a.hotel_id,c.item,a.go_end_time,(c.name) as hotel_name,c.type,c.number,(c.id) as hotel_id,a.go_status,a.back_start_time,a.back_status,a.go_start_addr,a.go_end_addr,a.go_flight_number,a.go_start_time,a.go_end_time,a.back_start_time,a.back_end_time,a.back_start_addr,a.back_end_addr,a.back_flight_number,d.arrange_time,(d.id) as car_id,(d.name) as driver_name,(d.number) as car_number')
            ->select();
        $this->assign('res', $res);
        return $this->fetch();
    }

    // 行程列表 - 送机
    public function carArrangeBack()
    {
        $res = db('meeting_travel')
            ->alias('a')
            ->join('applet_meeting_user w','w.id=a.user_id')
            ->join('applet_meeting_hotel c','c.id=a.hotel_id','LEFT')
            ->join('applet_meeting_car d','d.travel_id=a.id and d.purpose=1','LEFT')
            ->where(['a.admin_id'=>1004,'a.back_car'=>1])
            ->order('a.create_time desc')
            ->field('a.id,a.meeting_id,w.name,w.gender,w.company,w.id_card,w.mobile,a.go_start_date,a.back_end_date,a.create_time,a.hotel_id,c.item,a.go_end_time,(c.name) as hotel_name,c.type,c.number,(c.id) as hotel_id,a.go_status,a.back_start_time,a.back_status,a.go_start_addr,a.go_end_addr,a.go_flight_number,a.go_start_time,a.go_end_time,a.back_start_time,a.back_end_time,a.back_start_addr,a.back_end_addr,a.back_flight_number,d.arrange_time,(d.id) as car_id,(d.name) as driver_name,(d.number) as car_number')
            ->select();
        $this->assign('res', $res);
        return $this->fetch();
    }

    // 行程列表 - 其他用车
    public function carArrangeOther()
    {
        $res = db('meeting_car')->where(['admin_id'=>1004,'purpose'=>2])->select();
        $this->assign('res', $res);
        return $this->fetch();
    }

    // 安排用车
    public function travelCar($id,$purpose)
    {
        $carAll = db('meeting_user_car')->where(['admin_id'=>1004])->select();
        $info = db('meeting_travel')->where(['id'=>$id])->find();
        $hotel = db('meeting_hotel')->where(['id'=>$info['hotel_id']])->find();
        $this->assign('info', $info);
        $this->assign('car', $carAll);
        $this->assign('purpose', $purpose);
        if (request()->isPost()) {
            $post = input('post.');
            
            $car = db('meeting_user_car')->where(['id'=>$post['car_id']])->find();
            if ($purpose == 2) {
                $data['arrange_status'] = 1;
                $data['name'] = $car['name'];
                $data['mobile'] = $car['mobile'];
                $data['number'] = $car['number'];
                $data['color'] = $car['color'];
                $data['travel_id'] = $info['id'];

                $data['car_id'] = $post['car_id'];
                $res = db('meeting_car')->where(['id'=>$post['id']])->update($data);
                if ($res) {
                    echo '<script language="javascript">alert("编辑成功"); window.history.back(-1); </script> ';die;
                }else{
                    $this->error('修改失败');
                }
            }
            if ($purpose == 0) {
                $data['start_time'] = strtotime(date('Y-m-d',$info['go_start_date']).' '.$info['go_end_time']);
                $data['start_addr'] = $info['go_end_addr'];
                $data['end_addr'] = $hotel['name'];
                $data['go_contacts_name'] = $post['go_contacts_name'];
                $data['go_contacts_mobile'] = $post['go_contacts_mobile'];
            }elseif ($purpose == 1) {
                $data['start_time'] = strtotime(date('Y-m-d',$info['back_end_date']).' '.$info['back_start_time']);
                $data['start_addr'] =  $hotel['name'];
                $data['end_addr'] = $info['back_start_addr'];
                $data['arrange_time'] = strtotime($post['arrange_time']);
            }

            $data['admin_id'] = 1004;
            $data['car_id'] = $post['car_id'];
            $data['travel_id'] = $info['id'];
            $data['meeting_id'] = $info['meeting_id'];
            $data['user_id'] = $info['user_id'];
            $data['user_name'] = $info['name'];
            $data['user_mobile'] = $info['mobile'];
            
            $data['name'] = $car['name'];
            $data['mobile'] = $car['mobile'];
            $data['number'] = $car['number'];
            $data['color'] = $car['color'];
            $data['arrange_status'] = 1;
            
            $data['purpose'] = $purpose;
            $data['create_time'] = time();
            $res = db('meeting_car') ->insertGetId($data);
            if ($res) {
                db('meeting_car')->where(['id'=>$res])->update(['arrange_status'=>1]);
                if ($purpose == 0) {
                    db('meeting_travel')->where(['id'=>$id])->update(['go_status'=>1]);
                }elseif ($purpose == 1) {
                    db('meeting_travel')->where(['id'=>$id])->update(['back_status'=>1]);
                }
                $this->success('操作成功','meeting/carArrangeBack');
            }else{
                $this->error('修改失败');
            }
        }

        return $this->fetch();
    }

    // 编辑
    public function carArrangeEdit($id)
    {
        $info = db('meeting_car')->where(['id'=>$id])->find();
        $this->assign('info', $info);
        $car = db('meeting_user_car')->where(['admin_id'=>1004])->select();
        $this->assign('car', $car);
        $this->assign('info', $info);
        if (request()->isPost()) {
            $post = input('post.');
            if (!empty($post['car'])) {
                $car = db('meeting_user_car')->where(['id'=>$post['car']])->find();

                $data['car_id'] = $car['id'];
                $data['number'] = $car['number'];
                $data['color'] = $car['color'];
                $data['name'] = $car['name'];
                $data['mobile'] = $car['mobile'];
                
            }
            if (!empty($post['arrange_time'])) {
                $data['arrange_time'] = strtotime($post['arrange_time']);
            }
            if (!empty($post['go_contacts_name'])) {
                $data['go_contacts_name'] = $post['go_contacts_name'];

            }
            if (!empty($post['go_contacts_mobile'])) {
                $data['go_contacts_mobile'] = $post['go_contacts_mobile'];
            }
                        
            $res = db('meeting_car')->where(['id'=>$post['id']])->update($data);
            if ($res) {
                $this->success('操作成功','meeting/carArrangeGo');
            }else{
                $this->error('操作失败');
            }
        }
        return $this->fetch();
    }

    // 订单列表
    public function carListOrder()
    {
        $post = Request::instance()->param();
        $where = [];
        if(!empty($post['meeting'])){
            $where['meeting_id'] = ['like','%'.$post['meeting'].'%'];
        }
        $res = db('meeting_car')
            ->where(['admin_id'=>1004])
            ->where($where)
            ->field('user_name,user_mobile,purpose,name,mobile,color,number,start_time,arrange_time,start_addr,end_addr,status,meeting_id,create_time,arrange_status,id,eig_status')
            ->order('create_time desc')
            ->order('start_time desc')
            ->select();
        $meeting = !empty($post['meeting'])?$post['meeting']:'';
        $this->assign('meeting',$meeting);
        $this->assign('res', $res);
        return $this->fetch();
    }

    // 导出 - 车辆订单
    public function carDownload()
    {
        $post = Request::instance()->param();
        $where = [];
        //引入PHPExcel库文件
        Vendor('phpexcel.phpexcel');
        //创建对象
        $excel = new \PHPExcel();
        //Excel表格式
        $letter = array('A','B','C','D','E','F','G','H','I','j','k','L','M','N');
        //表头数组
        $tableheader = array('会议ID','嘉宾姓名','嘉宾电话','出发地','目的地','司机姓名','司机电话','车辆','车牌','用车时间','安排送机时间','用途','是否完成','是否空驶'); 

        //填充表头信息
        for($i = 0;$i < count($tableheader);$i++) {
        $excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");
        }

        //表格数组
        if (!empty($post['meeting'])) {
            $where['meeting_id'] = $post['meeting'];
        }

        $data = db('meeting_car')
            ->where(['admin_id'=>1004])
            ->where($where)
            ->field('meeting_id,user_name,user_mobile,start_addr,end_addr,name,mobile,color,number,start_time,arrange_time,purpose,status,eig_status')
            ->order('create_time desc')
            ->order('start_time desc')
            ->select();
            
        $meetingName = db('meeting_details')->where(['id'=>$post['meeting']])->find();
        if (empty($meetingName)) {
            $meetingName['title'] = '所有会议';
        }
        foreach ($data as $key => $value) {
            if (!empty($data[$key]['start_time'])) {
                $data[$key]['start_time'] = date('Y-m-d H:i',$value['start_time']);
            }

            if (!empty($data[$key]['arrange_time'])) {
                $data[$key]['arrange_time'] = date('Y-m-d H:i',$value['arrange_time']);
            }

            if (!empty($post['meeting'])) {
                $data[$key]['meeting_id'] = $meetingName['title'];
            }

            if ($data[$key]['status'] == 1) {
                $data[$key]['status'] = '是';
            }else{
                $data[$key]['status'] = '否';
            }

            if ($data[$key]['eig_status'] == 1) {
                $data[$key]['eig_status'] = '是';
            }else{
                $data[$key]['eig_status'] = '否';
            }
        }
        //填充表格信息
        for ($i = 2;$i <= count($data) + 1;$i++) {
            $j = 0;
            foreach ($data[$i - 2] as $key=>$value) {
            $excel->getActiveSheet()->setCellValue("$letter[$j]$i","$value".' ');
            $j++;
            }
        }

        //创建Excel输入对象
        $write = new \PHPExcel_Writer_Excel2007($excel);
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");;
        header('Content-Disposition:attachment;filename='.$meetingName['title'].'-车辆订单-'.date('Y-m-d',time()).'.xlsx');
        header("Content-Transfer-Encoding:binary");
        $write->save('php://output');
        exit;        
    }    

    // 完成订单
    public function orderStatus($id,$status)
    {
        $res = db('meeting_car')->where(['id'=>$id])->update(['status'=>$status,'order_time'=>time()]);
        if ($res) {
            $this->success('修改成功','meeting/carListOrder','',1);
        }else{
            $this->error('修改失败');
        }
    }

    // 车辆列表
    public function carList()
    {
        $res = db('meeting_user_car')->where(['admin_id'=>1004])->order('id desc')->select();
        
        foreach ($res as $k => $v){
            if ($v['gender'] == 1){
                $res[$k]['gender'] = "男";
            }else {
                $res[$k]['gender'] = "女";
            }
        }

        $this->assign('res', $res);
        return $this->fetch();
    }

    // 增加
    public function carAdd()
    {
        $master = db('user')->where(['status'=>2,'admin_id'=>1005])->select();
        $this->assign('master', $master);
        if (request()->isPost()) {
            $post = input('post.');
            $driver = db('user')->where(['id'=>$post['driver']])->find();
            $data['name'] = $driver['nickname'];
            $data['gender'] = $driver['gender'];
            $data['mobile'] = $driver['mobile'];
            $data['driver_id'] = $driver['id'];
            $data['color'] = $post['color'];
            $data['number'] = $post['number'];
            $data['admin_id'] = 1004;
            $data['create_time'] = time();
            $res = db('meeting_user_car')->insert($data);
            if ($res) {
                $this->success('添加成功','meeting/carList','',1);
            }else{
                $this->error('添加失败');
            }
        }

        return $this->fetch();
    }

    // 修改
    public function carEdit($id)
    {
        $info = db('meeting_user_car')->where(['id'=>$id])->find();
        $master = db('user')->where(['status'=>2])->select();
        $this->assign('master', $master);
        $this->assign('info', $info);
        if (request()->isPost()) {
            $post = input('post.');
            $data['number'] = $post['number'];
            $data['color'] = $post['color'];
            if (!empty($post['driver'])) {
                $driver = db('user')->where(['id'=>$post['driver']])->find();
                $data['name'] = $driver['nickname'];
                $data['gender'] = $driver['gender'];
                $data['mobile'] = $driver['mobile'];
            }
            $res = db('meeting_user_car')->where(['id'=>$post['id']])->update($data);
            if ($res) {
                $this->success('修改成功','meeting/carList','',1);
            }else{
                $this->error('修改失败');
            }
        }

        return $this->fetch();
    }

    // 删除
    public function carDel($id)
    {
        $res = db('meeting_user_car')->where(['id'=>$id])->delete();

        if ($res) {
            $this->success('删除成功','meeting/carList','',1);
        }else{
            $this->error('删除失败');
        }

    }

    // 酒店列表
    public function hotelList()
    {
        $res = db('meeting_hotel')->where(['admin_id'=>1004])->select();

        $this->assign('res', $res);

        return $this->fetch();
    }
    // 新增
    public function hotelAdd()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $data['admin_id'] = $this->accountId;
            $file = new \app\admin\logic\File();
            $data['thumb'] = $file->uploadFileAdmin($this->accountId);
            $data['name'] = $post['name'];
            $data['type'] = $post['type'];
            $data['number'] = $post['number'];
            $data['mobile'] = $post['mobile'];
            $data['address'] = $post['address'];
            if (!empty($post['position'])) {
                $position = explode(',',$post['position']);
                $data['longitude'] = $position[1];
                $data['latitude'] = $position[0];
            }
            $data['create_time'] = time();
            
            $res = db('meeting_hotel')->insert($data);
            if ($res) {
                $this->success('新增成功','meeting/hotelList');
            }else{
                $this->error('新增失败');
            }
        }

        return $this->fetch();
    }

    // 修改
    public function hotelEdit($id)
    {
        if (request()->isPost()) {
            $post = input('post.');

            $file = new \app\admin\logic\File();
            $data['thumb'] = $file->uploadFileAdmin(1004);
            if (empty($data['thumb'])) {
                unset($data['thumb']);
            }
            $data['name'] = $post['name'];
            $data['type'] = $post['type'];
            $data['number'] = $post['number'];
            $data['mobile'] = $post['mobile'];
            $position = explode(',',$post['position']);
            $data['longitude'] = $position[1];
            $data['latitude'] = $position[0];
            $data['address'] = $post['address'];
            
            $res = db('meeting_hotel')->where(['id'=>$post['id']])->update($data);
            if ($res) {
                $this->success('编辑成功','meeting/hotelList');
            }else{
                $this->error('编辑失败');
            }
        }
        $info = db('meeting_hotel')->where(['id'=>$id])->find();
        $info['position'] = $info['latitude'].','.$info['longitude'];
        $this->assign('info', $info);

        return $this->fetch();
    }

    // 删除
    public function hotelDel($id)
    {
        $res = db('meeting_hotel')->where(['id'=>$id])->delete();
        if ($res) {
            $this->success('删除成功','meeting/hotelList','',1);
        }else{
            $this->error('删除失败');
        }
    }

    //酒店订单
    public function hotelListOrder()
    {
        $res = db('meeting_travel')
            ->alias('a')
            ->join('meeting_hotel w','w.id=a.hotel_id','LEFT')
            ->where(['a.admin_id'=>1004,'a.hotel_id'=>['exp','is not null']])
            ->field('a.id,a.meeting_id,(a.name) as user_name,(a.mobile) as user_mobile,w.name,w.mobile,w.type,w.number,w.start_time,w.end_time,a.create_time,w.status')
            ->select();
        $this->assign('res', $res);
        return $this->fetch();
        
    }

    // 司机端账号
    public function masterAccount()
    {
        $res = db('user')->where(['admin_id'=>1005])->order('create_time desc')->select();

        $this->assign('res', $res);

        return $this->fetch();
    }

    // 修改
    public function masterEdit($id)
    {
        $userId = $id;
        if (request()->isPost()) {
            $post = input('post.');
            $data['nickname'] = $post['nickname'];
            $data['gender'] = $post['gender'];
            $data['mobile'] = $post['mobile'];

            $res = db('user')->where(['id'=>$post['id']])->update($data);
            if ($res) {
                $this->success('编辑成功','meeting/masterAccount');
            }else{
                $this->error('编辑失败');
            }
        }

        $info = db('user')->where(['id'=>$id])->find();
        $this->assign('info', $info);

        return $this->fetch();
    }

    // 审核
    public function masterStatus($id,$status)
    {
        $res = db('user')->where(['id'=>$id])->update(['status'=>$status]);
        if ($res) {
            $this->success('审核成功','meeting/masterAccount');
        }else{
            $this->error('审核失败');
        }
    }

    // 删除
    public function masterDel($id)
    {
        $res = db('user')->where(['id'=>$id])->delete();
        if ($res) {
            $this->success('删除成功','meeting/masterAccount');
        }else{
            $this->error('删除失败');
        }
    }

    // 员工端账号
    public function userAccount()
    {
        $res = db('user')->where(['admin_id'=>1003])->order('create_time desc')->select();

        $this->assign('res', $res);

        return $this->fetch();
    }

    // 修改
    public function userEdit($id)
    {
        $userId = $id;
        if (request()->isPost()) {
            $post = input('post.');
            $data['nickname'] = $post['nickname'];
            $data['gender'] = $post['gender'];
            $data['mobile'] = $post['mobile'];

            $res = db('user')->where(['id'=>$post['id']])->update($data);
            if ($res) {
                $this->success('编辑成功');
            }else{
                $this->error('编辑失败');
            }
        }

        $info = db('user')->where(['id'=>$id])->find();
        $this->assign('info', $info);

        return $this->fetch();
    }

    // 审核
    public function userStatus($id,$status)
    {
        $res = db('user')->where(['id'=>$id])->update(['status'=>$status]);
        if ($res) {
            $this->success('审核成功','meeting/userAccount','',1);
        }else{
            $this->error('审核失败');
        }
    }

    // 删除
    public function userDel($id)
    {
        $res = db('user')->where(['id'=>$id])->delete();
        if ($res) {
            $this->success('删除成功','meeting/userAccount','',1);
        }else{
            $this->error('删除失败','','',1);
        }
    }

    // 修改密码
    public function passwordEdit()
    {
        $admin = Session::get('user_id');
        $info = db('customer')->where(['id'=>$admin])->find();
        $this->assign('info', $info);
        if (request()->isPost()) {
            $post = input('post.');
            if (empty($post['old_password'])) {
                $this->error('请输入原密码','','',1);
            }
            if (md5($post['old_password']) != $info['pwd']) {
                $this->error('原密码错误','','',1);
            }
            if (empty($post['password'])) {
                $this->error('请输入密码','','',1);
            }
            if (empty($post['confirm_password'])) {
                $this->error('请确认密码','','',1);
            }
            if ($post['password'] != $post['confirm_password']) {
                $this->error('两次密码输入不一致','','',1);
            }

            $res = db('customer')->where(['id'=>$admin])->update(['pwd'=>md5($post['password'])]);
            if ($res) {
                $this->success('修改成功','meeting/passwordEdit','',1);
            }else{
                $this->error('修改失败');
            }
        }
        return $this->fetch();        
    }

    // 管理员
    public function adminList()
    {
        $res = db('meeting_admin')->where(['admin_id'=>1004])->select();
        foreach ($res as $key => $value) {
            $title = db('meeting_auth_list')->where(['id'=>$value['grade']])->find();
            $res[$key]['title'] = $title['title'];
        }
        $this->assign('res', $res);

        return $this->fetch();
    }

    // 新增
    public function adminAdd()
    {
        $info = db('meeting_auth_list')->select();
        $this->assign('info', $info);
        if (request()->isPost()) {
            $post = input('post.');
            $userAccount = db('meeting_admin')->where(['account'=>$post['account']])->find();
            if ($userAccount) {
                $this->error('该账已存在');
            }
            $post['admin_id'] = 1004;
            $post['create_time'] = time();
            if ($post['password'] != $post['confirm_password']) {
                $this->error('两次密码输入不一致');
            }else{
                unset($post['confirm_password']);
            }
            $post['password'] = md5($post['password']);

            $res = db('meeting_admin')->insert($post);

            if ($res) {
                $this->success('添加成功','meeting/adminList','',1);
            }else{
                $this->error('添加失败');
            }
        }

        return $this->fetch();
    }

    // 修改
    public function adminEdit($id)
    {
        if (request()->isPost()) {
            $post = input('post.');
            
            if ($post['password'] != $post['confirm_password']) {
                $this->error('两次密码输入不一致');
            }
            
            unset($post['confirm_password']);
            
            if (!empty($post['password'])) {
                $post['password'] = md5($post['password']);
            }
            
            $res = db('meeting_admin')->where(['id'=>$post['id']])->update($post);
            
            if ($res) {
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }

        $info = db('meeting_admin')->where(['id'=>$id])->find();
        $this->assign('info', $info);
        
        $grade = db('meeting_auth_list')->select();
        $this->assign('grade', $grade);
        
        return $this->fetch();
    }

    // 删除
    public function adminDel($id)
    {
        $res = db('meeting_admin')->where(['id'=>$id])->delete();

        if ($res) {
                $this->success('删除成功','meeting/adminList','',1);
            }else{
                $this->error('删除失败');
            }
    }

    
    /**
     * 机票/火车票管理
     */
    //正常机票账单
    public function planeOrder()
    {
        $res = db('meeting_travel')->where(['admin_id'=>1004,'vehicle'=>1])->order('create_time desc')->paginate($this->page, false);
        
        $page = $res->render();
        $this->assign('res', $res);
        $this->assign('page', $page);
        return $this->fetch();
    }
    
    //已退机票账单
    public function planeOrder_del()
    {
        $res = db('meeting_travel_copy')->where(['admin_id'=>1004,'vehicle'=>1])->order('create_time desc')->paginate($this->page, false);
        
        $page = $res->render();
        $this->assign('res', $res);
        $this->assign('page', $page);
        return $this->fetch();
    }
    
    //正常火车票账单
    public function trainorder()
    {
        $res = db('meeting_travel')->where(['admin_id'=>1004,'vehicle'=>2])->order('create_time desc')->paginate($this->page, false);
        
        $page = $res->render();
        $this->assign('res', $res);
        $this->assign('page', $page);
        return $this->fetch();
    }
    
    //已退火车票账单
    public function trainorder_del()
    {
        $res = db('meeting_travel_copy')->where(['admin_id'=>1004,'vehicle'=>2])->order('create_time desc')->paginate($this->page, false);
    
        $page = $res->render();
        $this->assign('res', $res);
        $this->assign('page', $page);
        return $this->fetch();
    }
    
    
    //短信发送
    public function sendNoteToMobile($mobile,$content)
    {
//         $post = request()->only(['mobile','content'], 'post');
    
//         // 手机号格式错误
//         if ( ! isMobile($post['mobile'])) {
//             err('mobile error');
//         }
    
           $verifyCode = mt_rand(1000, 9999);
    
//         if ($post['type'] == '1') {
//             $content = '您的验证码为' . $verifyCode;
//         } elseif ($post['type'] == 2) {
//             $content = '您的验证码为' . $verifyCode;
//         } elseif ($post['type'] == 3) {
//             $content = '您的验证码为' . $verifyCode;
//         }
        if (!isset($content)) {
            err('Type param error');
        }
    
        if ($mobile != '18711093783') {
            $url = 'http://smsapi.c123.cn/OpenPlatform/OpenApi'; //接口地址
            $ac = '1001@500863000001'; //用户账号
            $authkey = '11E42A616C08D53C8E2A2B5D2DE21000'; //认证密钥
            $cgid = '9384'; //通道组编号
            $c = $content; //内容
            $m = $mobile; //号码
            $csid = '9021'; //签名编号 ,可以为空时，使用系统默认的编号
            $t = ''; //发送时间,可以为空表示立即发送,yyyyMMddHHmmss 如:20130721182038
    
    
            $return = $this->sendSMS($url, $ac, $authkey, $cgid, $m, $c, $csid, $t);
        } else {
            $return = ['code'=>1, 'msg'=>'发送成功'];
        }
    
        // 短信发送失败
        if ($return['code'] != 1) {
            err($return['msg']);
        }
    
//         $resData = [];
//         $resData['mobile'] = $mobile;
//         $resData['verify_code'] = $verifyCode;
//         $resData['time'] = $this->timeStamp;
//         $resData['mid'] = md5($mobile . $verifyCode . $this->timeStamp . $this->signKey);
    
//         suc('success', $resData);
    }
    
    
    public function sendSMS($url, $ac, $authkey, $cgid, $m, $c, $csid, $t)
    {
        $data = array(
            //发送类型 ，可以有sendOnce短信发送，sendBatch一对一发送，sendParam	动态参数短信接口
            'action'=>'sendOnce',
            //用户账号
            'ac'=>$ac,
            //认证密钥
            'authkey'=>$authkey,
            //通道组编号
            'cgid'=>$cgid,
            //号码,多个号码用逗号隔开
            'm'=>$m,
            //'c'=>iconv('gb2312','utf-8',$c),
            //如果页面是gbk编码，则转成utf-8编码，如果是页面是utf-8编码，则不需要转码
            'c'=>$c,
    
            //签名编号 ，可以为空，为空时使用系统默认的签名编号
            'csid'=>$csid,
            //定时发送，为空时表示立即发送
            't'=>$t);
    
        $xml = $this->postSMS($url, $data); //POST方式提交
        $re = simplexml_load_string(utf8_encode($xml));
    
        //发送成功 ，返回企业编号，员工编号，发送编号，短信条数，单价，余额
    
        if (trim($re['result']) == 1) {
            foreach ($re->Item as $item) {
    
                $stat['msgid'] = trim((string)$item['msgid']);
                $stat['total'] = trim((string)$item['total']);
                $stat['price'] = trim((string)$item['price']);
                $stat['remain'] = trim((string)$item['remain']);
                $stat_arr[] = $stat;
            }
            if (is_array($stat_arr)) {
                return ['code'=>1, 'msg'=>'发送成功'];
                //echo "发送成功,返回值为" . $re['result'];
            }
        } else {
            $msg = '未知错误';
            //发送失败的返回值
            switch (trim($re['result'])) {
                case 0:
                    $msg =  "帐户格式不正确(正确的格式为:员工编号@企业编号)";
                    break;
                case  - 1:
                    $msg =  "服务器拒绝(速度过快、限时或绑定IP不对等)如遇速度过快可延时再发";
                    break;
                case  - 2:
                    $msg = " 密钥不正确";
                    break;
                case  - 3:
                    $msg = "密钥已锁定";
                    break;
                case  - 4:
                    $msg = "参数不正确(内容和号码不能为空，手机号码数过多，发送时间错误等)";
                    break;
                case  - 5:
                    $msg = "无此帐户";
                    break;
                case  - 6:
                    $msg = "帐户已锁定或已过期";
                    break;
                case  - 7:
                    $msg = "帐户未开启接口发送";
                    break;
                case  - 8:
                    $msg = "不可使用该通道组";
                    break;
                case  - 9:
                    $msg = "帐户余额不足";
                    break;
                case  - 10:
                    $msg = "内部错误";
                    break;
                case  - 11:
                    $msg = "扣费失败";
                    break;
                default:
                    break;
            }
    
            return ['code'=>trim($re['result']), 'msg'=>$msg];
        }
    
    }
    
    public function postSMS($url, $data = '')
    {
        $row = parse_url($url);
        $host = $row['host'];
        $port = 80;
        $file = $row['path'];
        $post = '';
    
        while (list($k, $v) = each($data)) {
            $post .= rawurlencode($k) . "=" . rawurlencode($v) . "&"; //转URL标准码
        }
        $post = substr($post, 0,  - 1);
        $len = strlen($post);
        $fp = @fsockopen($host, $port, $errno, $errstr, 10);
        if ( ! $fp) {
            return "$errstr ($errno)\n";
        } else {
            $receive = '';
            $out = "POST $file HTTP/1.0\r\n";
            $out .= "Host: $host\r\n";
            $out .= "Content-type: application/x-www-form-urlencoded\r\n";
            $out .= "Connection: Close\r\n";
            $out .= "Content-Length: $len\r\n\r\n";
            $out .= $post;
            fwrite($fp, $out);
            while ( ! feof($fp)) {
                $receive .= fgets($fp, 128);
            }
            fclose($fp);
            $receive = explode("\r\n\r\n", $receive);
            unset($receive[0]);
            return implode("", $receive);
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}